Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensor validations, scope changes, and TensorPrimitives methods #103005

Merged
merged 5 commits into from
Jun 7, 2024

Conversation

michaelgsharp
Copy link
Member

This PR adds in a bunch of additional validations for Tensors, TensorSpans, and ReadOnlyTensorSpans to make sure they are constructed correctly and that the associated lengths/strides are correct/safe/sensible.

Adds in the changes made by the scope analysis to add in or remove scoped as needed.

Adds all but a small handful of TensorPrimitives methods for the Tensors/TensorSpans.

This PR is blocked until #102998 gets merged in.

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

public static System.Numerics.Tensors.TensorSpan<T> AsTensorSpan<T>(this T[]? array, scoped System.ReadOnlySpan<nint> shape) { throw null; }
public static bool SequenceEqual<T>(this System.Numerics.Tensors.TensorSpan<T> span, System.Numerics.Tensors.TensorSpan<T> other) where T : System.IEquatable<T>? { throw null; }
public static System.Numerics.Tensors.TensorSpan<T> AbsInPlace<T>(System.Numerics.Tensors.TensorSpan<T> input) where T : System.IEquatable<T>, System.Numerics.IEqualityOperators<T, T, bool>, System.Numerics.INumberBase<T> { throw null; }
public static System.Numerics.Tensors.TensorSpan<T> Abs<T>(System.Numerics.Tensors.TensorSpan<T> input) where T : System.IEquatable<T>, System.Numerics.IEqualityOperators<T, T, bool>, System.Numerics.INumberBase<T> { throw null; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these inputs should be ReadOnlyTensorSpan to indicate they can't be mutated. They should also be scoped to indicate the return buffer won't be capturing the underlying byref.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not going to block the PR on this happening, so we can get the improvements in.

But we do need to fix it in a follow up.

@@ -43,7 +41,7 @@ namespace System.Numerics.Tensors
/// <param name="array">The target array.</param>
/// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
/// <exception cref="ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
public ReadOnlyTensorSpan(T[]? array) : this(array, 0, [], [])
public ReadOnlyTensorSpan(T[]? array) : this(array, 0, [array?.Length ?? 0], [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is one of the cases where for perf reasons we should have a simpler implementation in the constructor body rather than deferring down to the other overload

Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still more work to do, but this is an incremental improvement in the right direction

@michaelgsharp
Copy link
Member Author

wasm failures are known issues.

@michaelgsharp michaelgsharp merged commit 204f0e1 into dotnet:main Jun 7, 2024
80 of 83 checks passed
@michaelgsharp michaelgsharp deleted the tensor-validations branch June 7, 2024 22:52
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants